#include "TS.h"
#include "Podzial.h"
#include "Temp.h"
#include "Zarys.h"
#include "FilesManager.h"
#include "Global.h"

#ifdef GPU_INCLUDED
        #include "GPGPU.h"
#endif


TS::TS()
{
    
    glob = new Global();
#ifdef GPU_INCLUDED
    gpu = new GPGPU();
#endif
    ff = new FilesManager();
    zarys = new Zarys();
    t = new Temp();
}

TS::~TS()
{    
    delete t;
    delete zarys;
    delete ff;
#ifdef GPU_INCLUDED    
    delete gpu;
#endif    
    delete glob;

}


void TS::loadInput(const std::string& xdir, const std::string& xtyp)
{
  
    //std::cout << "Podaj nazwe zbioru z danymi: ";
    // std::cin >> typ;
    typ = xtyp;

    FM.SetDir(xdir);
    FM.LoadINP(typ);    
}


void TS::loadZarys()
{
        // ---------------    wczytanie zarysu szyny    --------------------

    zarys->czytaj(typ, GLOB.zargd.xp, GLOB.zargd.yp, GLOB.zargd.rp, GLOB.zargd.xrp,
           GLOB.zargd.yrp, GLOB.zargd.ttp, GLOB.zargd.alpp, GLOB.zargd.alkp);

}


int TS::loadPodzial(bool forceMesh)
{
    int needed = -1;
    //  -------------------   sprawdz, czy potrzebne meshowanie    ----------------------
    
    if (FM.isMeshingNeeded() || forceMesh)
    {
        std::cout << "Meshowanie ..." << std::endl;
        
        Podzial *podz = new Podzial();

        podz->podzial(GLOB.nkp.mw, GLOB.nkp.nw, GLOB.zargd.xpodz, GLOB.zargd.ypodz, &GLOB.nkp.nx,
                      GLOB.zargd.xp, GLOB.zargd.yp, GLOB.zargd.rp, GLOB.zargd.xrp, GLOB.zargd.yrp,
                      GLOB.zargd.ttp, GLOB.zargd.alpp, GLOB.zargd.alkp, GLOB.zargd.xw,
                      &GLOB.nkp.xcharl, &GLOB.nkp.ycharl, &GLOB.nkp.xchar, &GLOB.nkp.ychar,
                      &GLOB.nkp.xchard, &GLOB.nkp.ychard, &GLOB.nkp.xcharld, &GLOB.nkp.ycharld, typ);

        FM.SaveMSH();
        
        std::cout << "Koniec meshowania" << std::endl;

        delete podz;
        
        needed = 1;

    }
    else
    {
        std::cout << "Meshowanie nie jest potrzebne" << std::endl;
        FM.LoadMSH();
        
        needed = 2;
    }
    
    FM.SaveMSH000(typ, GLOB.zargd.xw, GLOB.nkp.mw, GLOB.nkp.nw);
    FM.SaveBLNs(typ); 
    
    return needed;
}

   
void TS::loadTemp()
{
    
    //  -------------------   obliczenia temperatury    ----------------------
    
    double dtemgrd = 0.0;
    double dtempgrd = 0.0;
    int idt;    

    std::cout << std::endl;
    for (int ich = 0; ich < GLOB.ichl; ++ich) // ilosc chlodzen
    {
        std::cout << "Chlodzenie: " << ich+1 << "/" << GLOB.ichl << " ... " << std::endl;

        // TODO 5 i 6 argument mozna chyba usunac
        t->temper(GLOB.ral.ispray, ich, GLOB.nkp.mw, GLOB.nkp.nw, GLOB.v_ndt[ich], GLOB.v_dt[ich],
               GLOB.iprint, &idt, GLOB.istem, GLOB.temgrd, &dtemgrd, GLOB.iwn, GLOB.tempgrd, &dtempgrd);

        v_idt.push_back(idt);
        v_tpc.push_back(GLOB.tpc);

    }
    std::cout << "Koniec obliczen" << std::endl;
    std::cout << "LICZ " << GLOB.LICZ << std::endl;
    
}

void TS::saveFiles()
{
    
    FM.SavePLG(typ, t->v_plg);
    FM.SaveTLG(typ, GLOB.istem, t->v_tlg);
    FM.SaveDATs(typ, GLOB.ichl, GLOB.iw, t->v_tf);
    FM.SaveTEMLOGs(GLOB.ichl, v_idt, v_tpc);
    FM.SaveOUT(GLOB.iwkod, GLOB.iw, GLOB.ie, GLOB.ichl, GLOB.nkp.mw, GLOB.nkp.nw, GLOB.v_ndt, t->v_tchcal, t->v_t, t->v_show_temps); 

}
